﻿using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Silk.NET.Vulkan;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;

namespace VkSample43
{
    public class GroupItemMiddleware : IProductionLineMiddleware<VulkanContext>
    {
        public void Invoke(VulkanContext context, RenderingProductionLineDelegate<VulkanContext> next)
        {
            var pipeline = context.VulkanPipeline;
            var vro = context.GetContextRenderObject().GetComponents<VRO>().First();
            ref MVPMatrix local = ref context.GetMVPMatrix();
            Matrix4x4 mat = local.ModelMatrix * local.ViewMatrix * local.ProjectionMatrix * context.ClipMatrix;
            byte[] data = mat.ToBytes();

            context.CommandBuffer.BindVertexBuffers(0, stackalloc ulong[] { 0 }, vro.VerticesBufferInfo.Buffer);
            context.CommandBuffer.PushConstants(pipeline.PipelineLayout, ShaderStageFlags.ShaderStageVertexBit, 0, data);
            context.CommandBuffer.Draw(vro.VecticesCount, 1, 0, 0);
            next(context);
        }
    }
}
